package com.eianiao.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eianiao.bean.Dish;
import com.eianiao.common.PageParam;
import com.eianiao.common.R;
import com.eianiao.dao.DishDao;
import com.eianiao.dto.DishDto;
import com.eianiao.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author eianiao
 * @Date 2022 06
 * @Version 1.0
 **/
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 添加菜品
     * @param dishDto
     * @return
     */
    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        System.out.println("-----------------");
        System.out.println(dishDto);
        System.out.println("-----------------");


        int rows = dishService.add(dishDto);
        System.out.println("-----------------");
        System.out.println(dishDto);
        System.out.println("-----------------");

        if (rows > 0){
            return R.success("添加成功！");
        }
        return R.error("添加失败！");
    }


    /**
     * 分页查询菜品数据
     * @param pageParam
     * @return
     */
    @GetMapping("/page")
    public R findPage(PageParam pageParam){

        try {
            //1. 调用service
            IPage<DishDto> page = dishService.findPage(pageParam);

            //2. 响应
            return R.success(page);
        } catch (Exception e) {
            e.printStackTrace();

            return R.error("分页查询失败！");
        }
    }


    /**
     * 更新菜品
     * @param dishDto
     * @return
     */
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        int row = dishService.update(dishDto);
        if (row > 0){
            return R.success("更新成功！");
        }
        return R.error("更新失败！");

    }

    /**
     * 查询套餐分类的菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public R findByCategoryId(Long categoryId,Integer status,String name){
        List<DishDto> dishList = dishService.findByCategoryId(categoryId,status,name);
        return R.success(dishList);
    }

    /**
     * 根据id更新菜品
     * @param statusId
     * @param ids
     * @return
     */
    @PostMapping("/status/{statusId}")
    public R updateById(@PathVariable int statusId,@RequestParam List<Long> ids){

        int row = dishService.updateById(statusId,ids);
        if (row > 0){
            return R.success("更新菜品成功！");
        }
        return R.error("更新菜品失败！");

    }

    /**
     * 根据id删除或者批量删除
     * @param ids
     * @return
     */
    @DeleteMapping
    public R delete(@RequestParam List<Long> ids){
        int row = dishService.deleteById(ids);
        if (row > 0){
            return R.success("删除菜品成功！");
        }
        return R.error("删除菜品失败！");



    }



}
